package com.klangner.behaviors
{
  import com.klangner.geom.Vector2D;
  import com.klangner.move.behavior.ArriveBehavior;
  import com.klangner.move.behavior.FleeBehavior;
  import com.klangner.move.behavior.IBehavior;
  import com.klangner.move.behavior.RectangleBehavior;
  import com.klangner.move.behavior.SeekBehavior;
  import com.klangner.move.behavior.WanderBehavior;
  import com.klangner.utils.Color;
  
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.geom.Rectangle;
  
  import mx.containers.Canvas;

  public class BehaviorsView extends Canvas
  {
    private var _followMouseBehavior:SeekBehavior;
    private var _vehicles:Array = new Array();
    

    public function init():void{
      
      _followMouseBehavior = new SeekBehavior();
//      initSeek();
//      initSeekFlee();
//      initSeekers();
      initWander();
      this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    // ------------------------------------------------------------------------
    private function initWander():void{
      
      var vehicle:SteeredVehicle;
      var boundsBehavior:IBehavior = new RectangleBehavior(
                new Rectangle(100, 100, width-200, height-200));
      
      for(var i:int = 0; i < 20; i++){

        vehicle = new SteeredVehicle();
        vehicle.position = new Vector2D(Math.random()*width, Math.random()*height);
//        vehicle.addBehavior(boundsBehavior);
        vehicle.addBehavior(new WanderBehavior());
        vehicle.color = Color.randomColor();
        _vehicles.push(vehicle);
        this.rawChildren.addChild(vehicle);
      }

    }
    
    // ------------------------------------------------------------------------
    private function initSeek():void{
      
      var vehicle:SteeredVehicle = new SteeredVehicle();
      
      vehicle.position = new Vector2D(Math.random()*width, Math.random()*height);
      vehicle.addBehavior(_followMouseBehavior);
      _vehicles.push(vehicle);
      this.rawChildren.addChild(vehicle);

    }
    
    // ------------------------------------------------------------------------
    private function initSeekFlee():void{
      
      var center:Vector2D = new Vector2D(width/2, height/2);
      var range:Number = 100;
      var vehicle:SteeredVehicle = new SteeredVehicle();
      
      vehicle.position = new Vector2D(Math.random()*width, Math.random()*height);
      vehicle.addBehavior(_followMouseBehavior);
      vehicle.addBehavior(new FleeBehavior(center, range));
      _vehicles.push(vehicle);
      this.rawChildren.addChild(vehicle);

      var obstacle:Circle = new Circle(center);
      obstacle.radius = range;
      this.rawChildren.addChild(obstacle);

    }
    
    // ------------------------------------------------------------------------
    private function initSeekers():void{
      
      var red:SteeredVehicle = new SteeredVehicle();
      var blue:SteeredVehicle = new SteeredVehicle();
      var green:SteeredVehicle = new SteeredVehicle();
      var yellow:SteeredVehicle = new SteeredVehicle();
      var bounds:IBehavior = new RectangleBehavior(new Rectangle(100, 100, width-200, height-200));
      
      red.color = 0xff0000;
      red.position = new Vector2D(Math.random()*width, Math.random()*height);
      red.addBehavior(new SeekVehicleBehavior(blue));
      red.addBehavior(new FleeVehicleBehavior(yellow));
      red.edgeBehavior = Vehicle.BOUNCE;
      _vehicles.push(red);
      this.rawChildren.addChild(red);

      blue.color = 0x0000ff;
      blue.position = new Vector2D(Math.random()*width, Math.random()*height);
      blue.addBehavior(new SeekVehicleBehavior(green));
      blue.addBehavior(new FleeVehicleBehavior(red));
      blue.edgeBehavior = Vehicle.BOUNCE;
      _vehicles.push(blue);
      this.rawChildren.addChild(blue);

      green.color = 0x00ff00;
      green.position = new Vector2D(Math.random()*width, Math.random()*height);
      green.addBehavior(new SeekVehicleBehavior(yellow));
      green.addBehavior(new FleeVehicleBehavior(blue));
      green.edgeBehavior = Vehicle.BOUNCE;
      _vehicles.push(green);
      this.rawChildren.addChild(green);

      yellow.color = 0xffff00;
      yellow.position = new Vector2D(Math.random()*width, Math.random()*height);
      yellow.addBehavior(new SeekVehicleBehavior(red));
      yellow.addBehavior(new FleeVehicleBehavior(green));
      yellow.edgeBehavior = Vehicle.BOUNCE;
      _vehicles.push(yellow);
      this.rawChildren.addChild(yellow);

    }
    
    // ------------------------------------------------------------------------
    public function initArrive():void{
      
      var vehicle:SteeredVehicle = new SteeredVehicle();
      var center:Vector2D = new Vector2D(width/2, height/2);
      var range:Number = 100;
      
      vehicle.position = new Vector2D(Math.random()*width, Math.random()*height);
      vehicle.addBehavior(new ArriveBehavior(center, range));
      _vehicles.push(vehicle);
      this.rawChildren.addChild(vehicle);

      var obstacle:Circle = new Circle(center);
      obstacle.radius = range;
      this.rawChildren.addChild(obstacle);

      this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    
    // ------------------------------------------------------------------------
    private function onEnterFrame(event:Event):void {
      _followMouseBehavior.target = new Vector2D(mouseX, mouseY);
      
      for each(var vehicle:Vehicle in _vehicles){
        vehicle.update();
      }
      
    }

    // ------------------------------------------------------------------------
    private function onMouseDown(event:MouseEvent):void {
      for each(var vehicle:Vehicle in _vehicles){
        vehicle.position = new Vector2D(mouseX, mouseY);;
      }
    }
  }
}